package com.woniu.adapter;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.outlet.dao.WorkingProcedurePoMapper;
import com.woniu.outlet.po.WorkingProcedurePo;
import com.woniu.outlet.rabbitmq.MessageRepo;
import com.woniu.outlet.redis.RedisRepo;
import com.woniu.utils.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class WorkingProcedureAdapter {

    @Autowired
    private WorkingProcedurePoMapper workingProcedurePoMapper;

    @Autowired
    private RedisRepo redisRepo;

    @Autowired
    private MessageRepo messageRepo;


    public List<WorkingProcedurePo> findWorkingProcedures() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("workingProcedure:list");
        List<WorkingProcedurePo> lists = new ArrayList<>();
        if (!StringUtils.isEmpty(str)) {
            lists = mapper.readValue(str, new TypeReference<List<WorkingProcedurePo>>() {
            });
            return lists;
        }
        lists = workingProcedurePoMapper.selectByExample(null);
        //存到redis
        redisRepo.saveRedisRepo("workingProcedure:list",mapper.writeValueAsString(lists));
        return lists;
    }

    public WorkingProcedurePo getWorkingProcedureById(Long id) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("workingProcedure:" + id);
        WorkingProcedurePo workingProcedurePo = null;
        if (!StringUtils.isEmpty(str)) {
            workingProcedurePo = mapper.readValue(str, new TypeReference<WorkingProcedurePo>() {
            });
            return workingProcedurePo;
        }
        workingProcedurePo = workingProcedurePoMapper.selectByPrimaryKey(id);
        //存到redis
        redisRepo.saveRedisRepo("workingProcedure:"+id,mapper.writeValueAsString(workingProcedurePo));
        return workingProcedurePo;
    }

    public WorkingProcedurePo getWorkingProcedureByWorkingProcedureCode(String code) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("workingProcedure:" + code);
        WorkingProcedurePo workingProcedurePo = null;
        if (!StringUtils.isEmpty(str)) {
            workingProcedurePo = mapper.readValue(str, new TypeReference<WorkingProcedurePo>() {
            });
            return workingProcedurePo;
        }
        workingProcedurePo = workingProcedurePoMapper.selectByWorkingProcedureCode(code);
        //存到redis
        redisRepo.saveRedisRepo("workingProcedure:"+code,mapper.writeValueAsString(workingProcedurePo));
        return workingProcedurePo;
    }

    public WorkingProcedurePo getByCode(String code) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("workingProcedure:" + code);
        WorkingProcedurePo workingProcedurePo = null;
        if (!StringUtils.isEmpty(str)) {
            workingProcedurePo = mapper.readValue(str, new TypeReference<WorkingProcedurePo>() {
            });
            return workingProcedurePo;
        }
        workingProcedurePo = workingProcedurePoMapper.getByCode(code);
        //存到redis
        redisRepo.saveRedisRepo("workingProcedure:"+code,mapper.writeValueAsString(workingProcedurePo));
        return workingProcedurePo;
    }



    public void addWorkingProcedure(WorkingProcedurePo workingProcedurePo){
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        long id = snowflakeIdGenerator.nextId();
        workingProcedurePo.setWorkingProcedureCode("GX"+id);
        workingProcedurePoMapper.insert(workingProcedurePo);
        redisRepo.deleteRedisRepo("workingProcedure:list");

    }

    public void updateWorkingProcedure(WorkingProcedurePo workingProcedurePo){
        workingProcedurePoMapper.updateByPrimaryKeySelective(workingProcedurePo);
        redisRepo.deleteRedisRepo("workingProcedure:list");
    }

    public void deleteWorkingProcedureById(Long id){
        workingProcedurePoMapper.deleteByPrimaryKey(id);
        redisRepo.deleteRedisRepo("workingProcedure:list");
    }



}
